from django.test import TestCase
import json
import uuid
from .models import ShortMessage, getById, getByMessageId, getList
from .views import parse_alertmanage_msg
import datetime

# Create your tests here.
class SmsTestCase(TestCase):
    def test_insert_shortMessage(self):
        instance = ShortMessage()
        instance.messageId=str(uuid.uuid4()).replace("-", "")
        instance.phoneNumber="10010"
        instance.content="test content"
        instance.send_time = datetime.datetime.now()
        instance.status = "success"
        print("shortmessage instance:", instance.messageId)
        instance.save()
        # get by id
        getById(instance.id)
        # get by messageId
        getByMessageId(instance.messageId)
        # get list
        getList()

        instance2 = ShortMessage(messageId=str(uuid.uuid4()).replace("-", ""), phoneNumber="10000")
        print("instance2: ", instance2)

    def test_parse_alertmsg(self):
        params = {
            "receiver":"ops",
            "status":"firing",
            "alerts":[
                {
                    "status":"firing",
                    "labels":{
                        "alertname":"load5负载高于5",
                        "beta_kubernetes_io_arch":"amd64",
                        "beta_kubernetes_io_fluentd_ds_ready":"true",
                        "beta_kubernetes_io_os":"linux",
                        "instance":"test-k8s-master",
                        "job":"node_exporter",
                        "kubernetes_io_arch":"amd64",
                        "kubernetes_io_hostname":"test-k8s-master",
                        "kubernetes_io_os":"linux",
                        "severity":"critical",
                        "team":"ops"
                    },
                    "annotations":{
                        "description":"test-k8s-master 高于5,当前值:0.56",
                        "summary":"load5负载高于5"
                    },
                    "startsAt":"2020-12-30T05:04:49.455639638Z",
                    "endsAt":"0001-01-01T00:00:00Z",
                    "generatorURL":"http://xxx.com/graph?g0.expr=node_load5+%3E+0&amp;g0.tab=1"
                },
                {
                    "status":"firing",
                    "labels":{
                        "alertname":"load5负载高于5",
                        "beta_kubernetes_io_arch":"amd64",
                        "beta_kubernetes_io_fluentd_ds_ready":"true",
                        "beta_kubernetes_io_os":"linux",
                        "instance":"test-k8s-node1",
                        "job":"node_exporter",
                        "kubernetes_io_arch":"amd64",
                        "kubernetes_io_hostname":"test-k8s-node1",
                        "kubernetes_io_os":"linux",
                        "severity":"critical",
                        "team":"ops"
                    },
                    "annotations":{
                        "description":"test-k8s-node1 高于5,当前值:1.42",
                        "summary":"load5负载高于5"
                    },
                    "startsAt":"2020-12-30T05:04:49.455639638Z",
                    "endsAt":"0001-01-01T00:00:00Z",
                    "generatorURL":"http://xxx.com/graph?g0.expr=node_load5+%3E+0&amp;g0.tab=1"
                },
                {
                    "status":"firing",
                    "labels":{
                        "alertname":"load5负载高于5",
                        "beta_kubernetes_io_arch":"amd64",
                        "beta_kubernetes_io_fluentd_ds_ready":"true",
                        "beta_kubernetes_io_os":"linux",
                        "instance":"test-k8s-node2",
                        "job":"node_exporter",
                        "kubernetes_io_arch":"amd64",
                        "kubernetes_io_hostname":"test-k8s-node2",
                        "kubernetes_io_os":"linux",
                        "severity":"critical",
                        "team":"ops"
                    },
                    "annotations":{
                        "description":"test-k8s-node2 高于5,当前值:0.16",
                        "summary":"load5负载高于5"
                    },
                    "startsAt":"2020-12-30T05:04:49.455639638Z",
                    "endsAt":"0001-01-01T00:00:00Z",
                    "generatorURL":"http://xxx.com/graph?g0.expr=node_load5+%3E+0&amp;g0.tab=1"
                }
            ],
            "groupLabels":{
                "alertname":"load5负载高于5"
            },
            "commonLabels":{
                "alertname":"load5负载高于5",
                "severity":"critical",
                "team":"ops"
            },
            "commonAnnotations":{
                "summary":"load5负载高于5"
            },
            "externalURL":"http://alertmanager-57fffcb99c-kjhcj:9093",
            "version":"4",
            "groupKey":"{}/{severity=\"critical\"}:{alertname=\"load5负载高于5\"}"
        }
        msg_list = parse_alertmanage_msg(params)

    def test_parse_resolvemsg(self):
        params = {
            "receiver":"ops",
            "status":"resolved",
            "alerts":[
                {
                    "status":"resolved",
                    "labels":{
                        "alertname":"load5负载高于5",
                        "beta_kubernetes_io_arch":"amd64",
                        "beta_kubernetes_io_fluentd_ds_ready":"true",
                        "beta_kubernetes_io_os":"linux",
                        "instance":"test-k8s-master",
                        "job":"node_exporter",
                        "kubernetes_io_arch":"amd64",
                        "kubernetes_io_hostname":"test-k8s-master",
                        "kubernetes_io_os":"linux",
                        "severity":"critical",
                        "team":"ops"
                    },
                    "annotations":{
                        "description":"test-k8s-master 高于5,当前值:0.09",
                        "summary":"load5负载高于5"
                    },
                    "startsAt":"2020-12-30T05:04:49.455639638Z",
                    "endsAt":"2020-12-30T06:07:49.455639638Z",
                    "generatorURL":"http://119.3.44.21:30002/graph?g0.expr=node_load5+%3E+5&amp;g0.tab=1"
                },
                {
                    "status":"resolved",
                    "labels":{
                        "alertname":"load5负载高于5",
                        "beta_kubernetes_io_arch":"amd64",
                        "beta_kubernetes_io_fluentd_ds_ready":"true",
                        "beta_kubernetes_io_os":"linux",
                        "instance":"test-k8s-node1",
                        "job":"node_exporter",
                        "kubernetes_io_arch":"amd64",
                        "kubernetes_io_hostname":"test-k8s-node1",
                        "kubernetes_io_os":"linux",
                        "severity":"critical",
                        "team":"ops"
                    },
                    "annotations":{
                        "description":"test-k8s-node1 高于5,当前值:0.61",
                        "summary":"load5负载高于5"
                    },
                    "startsAt":"2020-12-30T05:04:49.455639638Z",
                    "endsAt":"2020-12-30T06:07:49.455639638Z",
                    "generatorURL":"http://119.3.44.21:30002/graph?g0.expr=node_load5+%3E+5&amp;g0.tab=1"
                },
                {
                    "status":"resolved",
                    "labels":{
                        "alertname":"load5负载高于5",
                        "beta_kubernetes_io_arch":"amd64",
                        "beta_kubernetes_io_fluentd_ds_ready":"true",
                        "beta_kubernetes_io_os":"linux",
                        "instance":"test-k8s-node2",
                        "job":"node_exporter",
                        "kubernetes_io_arch":"amd64",
                        "kubernetes_io_hostname":"test-k8s-node2",
                        "kubernetes_io_os":"linux",
                        "severity":"critical",
                        "team":"ops"
                    },
                    "annotations":{
                        "description":"test-k8s-node2 高于5,当前值:0.4",
                        "summary":"load5负载高于5"
                    },
                    "startsAt":"2020-12-30T05:04:49.455639638Z",
                    "endsAt":"2020-12-30T06:07:49.455639638Z",
                    "generatorURL":"http://119.3.44.21:30002/graph?g0.expr=node_load5+%3E+5&amp;g0.tab=1"
                }
            ],
            "groupLabels":{
                "alertname":"load5负载高于5"
            },
            "commonLabels":{
                "alertname":"load5负载高于5",
                "severity":"critical",
                "team":"ops"
            },
            "commonAnnotations":{
                "summary":"load5负载高于5"
            },
            "externalURL":"http://alertmanager-57fffcb99c-kjhcj:9093",
            "version":"4",
            "groupKey":"{}/{severity=\"critical\"}:{alertname=\"load5负载高于5\"}"
        }
        msg_list = parse_alertmanage_msg(params)